#include "cpu_monitor_gui.h"
#include "w_cpuid.h"
#include "../cpu_parse_display/cpu_cpuid.h"

 	const char *w_generic_cap_flags[] = {
		"fpu", "vme", "de", "pse", "tsc", "msr", "pae", "mce",
		"cx8", "apic", "\0", "sep", "mtrr", "pge", "mca", "cmov",
		"pat", "pse36", "psn", "clflsh", "\0", "dtes", "acpi", "mmx",
		"fxsr", "xmm", "xmm2", "selfsnoop", "ht", "acc", "ia64", "\0"
	};


CpuidTab::CpuidTab(QWidget *parent)
     : MonitorPageTab(parent)
 {
    
	cpuid_identity = new RegWin(QString("Identity") ,7);
    cpuid_0   = new RegWin(QString("for eax =0") ,4);
    cpuid_1   = new RegWin(QString("for eax =1") ,4 );
    cpuid_2   = new RegWin(QString("for eax =2") ,4);
   
    QHBoxLayout *cpuidRegsLayout = new QHBoxLayout;
    cpuidRegsLayout->addWidget(cpuid_0);
    cpuidRegsLayout->addWidget(cpuid_1);
    cpuidRegsLayout->addWidget(cpuid_2);
     
    QGridLayout *featuresGrid = new QGridLayout();
    int i, row, column;
    for (i =0,  row =0 ; row < 8 ; row++ ){
		for( column =0 ; column < 4; column++ , i++){
			if (w_generic_cap_flags[i] ){
				feature_indicator[i] = new QLabel( w_generic_cap_flags[i]  ) ;
			}else{ 
				feature_indicator[i] = new QLabel( "-" ) ;
			}
			featuresGrid->addWidget(feature_indicator[i], row, column);
		}
	}
     
     
	 QGroupBox  *featuresGroup   = new QGroupBox("Features:");
  	 featuresGroup->setLayout(featuresGrid);
    
 
     QVBoxLayout *mainLayout = new QVBoxLayout;
     mainLayout->addWidget(cpuid_identity);
     mainLayout->addLayout(cpuidRegsLayout);
     mainLayout->addWidget(featuresGroup);
     mainLayout->addStretch(1); 
     setLayout(mainLayout);
     
     update();
     
 }


int CpuidTab::update(void){
	//int cpu =0;
	cpuid_data_s cpu_data;
	unsigned long eax,ebx, ecx,edx;
	
	if ( identify (fd[CurrCpu],&cpu_data) ){
		fprintf(stderr, "\nGUI:failed to get identify!  CurrCpu=%d fd[CurrCpu]=%d \n", CurrCpu, fd[CurrCpu]);
		return -1;
	}
	
	int i = 0 ;
	cpuid_identity->set_regs( i++, "Name:", cpu_data.name);
	cpuid_identity->set_regs( i++,"Model:",    QString("%1").arg (cpu_data.vendor));
	cpuid_identity->set_regs( i++,"Family:",   QString("%1").arg (cpu_data.family));    
	cpuid_identity->set_regs( i++, "Model:",   QString("%1").arg (cpu_data.model));     
	cpuid_identity->set_regs( i++,"Stepping:", QString("%1").arg (cpu_data.stepping));  
	cpuid_identity->set_regs( i++, "Type:",    QString("%1").arg (cpu_data.type));      
	cpuid_identity->set_regs( i++,"Brand:",    QString("%1").arg (cpu_data.brand)); 
	
	
	
	if ( read_cpuid (fd[CurrCpu], 0, &eax,&ebx, &ecx,&edx) ){
		 fprintf(stderr, "\nGUI:failed to get cpuid 0!\n");
		return -1;
	}
	cpuid_0->set_ex_regs(eax,ebx,ecx,edx);
	
	if ( read_cpuid (fd[CurrCpu], 1, &eax,&ebx, &ecx,&edx) ){
		 fprintf(stderr, "\nGUI:failed to get cpuid 1!\n");
		return -1;
	}
	cpuid_1->set_ex_regs(eax,ebx,ecx,edx);
	update_features(edx);
	
	
	if ( read_cpuid (fd[CurrCpu], 2, &eax,&ebx, &ecx,&edx) ){
		fprintf(stderr, "\nGUI:failed to get cpuid 2!\n");
		return -1;
	}
	cpuid_2->set_ex_regs(eax,ebx,ecx,edx);
	
	
 
	return 0;    
}

int CpuidTab::update_features(unsigned long edx){
	int i, row, column;

	for (i =0,  row =0 ; row < 8 ; row++ ){
		for( column =0 ; column < 4; column++ , i++){
			//PDEBUG_DISP("row%d column%d, %d",row, column, i);
			if (w_generic_cap_flags[i] ){
				if (edx & (1 << i)){
			 	 	feature_indicator[i]->setFrameStyle(QFrame::Panel | QFrame::Sunken);
			 	 	feature_indicator[i]->setLineWidth(2);
				}
			}	
		}
	}
	return 0;
}

